import { EventInput } from "@fullcalendar/core";
import axios from "../utils/request";

let eventGuid = 0;
let todayStr = new Date().toISOString().replace(/T.*$/, ""); // YYYY-MM-DD of today

export let INITIAL_EVENTS: EventInput[] = [];

// 获取日历会议数据并处理
function fetchAndProcessConferenceData() {
  axios.get("/conference/viewsets/")
    .then((response) => {
      const conferenceData = response.data.results;

      const processedEvents = conferenceData.map((i: { title: string, preset_time: string }) => {
        return {
          id: createEventId(),
          title: i.title,
          start: i.preset_time.replace(" ", "T")
        };
      });

      INITIAL_EVENTS = processedEvents;
      console.log(INITIAL_EVENTS);

      // 在数据处理完成后进行页面展示或其他操作
      enablePageFunctionality();
    })
    .catch((error) => {
      console.error("Error fetching and processing conference data:", error);
    });
}

function enablePageFunctionality() {
  // 启用页面相关功能，展示数据等
  console.log("Page functionality enabled");
}

export function createEventId() {
  return String(eventGuid++);
}

// 调用获取和处理日历会议数据的函数
fetchAndProcessConferenceData();